package com.yansu.cloud;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * 
 * @author 言肃
 * @email yansu_code@126.com
 * @version 1.0
 * @date Aug 13, 2018 6:13:56 PM
 */
public class PreZuulFilter extends ZuulFilter {

	// 创建Logger对象
	private static final Logger LOGGER = LoggerFactory.getLogger(PreZuulFilter.class);

	/**
	 * @return 是否执行PreZuulFilter结果，默认为false不执行，需要配置true执行
	 * @see com.netflix.zuul.IZuulFilter#shouldFilter()
	 */
	@Override
	public boolean shouldFilter() {
		return true;
	}

	/**
	 * 当前PreZuulFilter的具体代码逻辑
	 * 
	 * @return
	 * @see com.netflix.zuul.IZuulFilter#run()
	 */
	@Override
	public Object run() {
		HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
		String host = request.getRemoteHost();
		PreZuulFilter.LOGGER.info("请求的Host为: " + host);
		return null;
	}

	/**
	 * @return 当前PreZuulFilter的类型，取值为"pre","route","post","error"
	 * @see com.netflix.zuul.ZuulFilter#filterType()
	 */
	@Override
	public String filterType() {
		return "pre";
	}

	/**
	 * @return 当前PreZuulFilter执行优先级，数字越大，优先级越低
	 * @see com.netflix.zuul.ZuulFilter#filterOrder()
	 */
	@Override
	public int filterOrder() {
		return 1;
	}

}
